﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ecopsy.Tools;

public partial class Ctl_ctlPeriod : System.Web.UI.UserControl
{
    public class PeriodFiltr
    {
        public  DateTime _Start = DateTimeTools.GetFisrtDayInMonth (DateTime.Now);
        public DateTime _Finish = DateTimeTools.GetLastDayInMonth (DateTime.Now);
        public DateTimeTools.TimePeriod _ShedulePeriod = DateTimeTools.TimePeriod.Month;
        public bool _ShowOnlySelected = false;
        public string  _Lexeme= string.Empty;
    }

    protected  PeriodFiltr _PeriodFiltr= new PeriodFiltr (); 
 
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }

    public DateTime Start
    {
        get
        {
            try
            {
                _PeriodFiltr._Start = Convert.ToDateTime(txtStart.Text);
                return _PeriodFiltr._Start;
            }
            catch
            {
                _PeriodFiltr._Start = DateTimeTools.GetLastDayInMonth(DateTime.Now);
                return _PeriodFiltr._Start;
            }

        }

        set {
                txtStart.Text= value.ToShortDateString() ;
                _PeriodFiltr._Start = value; 
            }
    }

    public DateTime Finish
    {
        get
        {
            try
            {
                _PeriodFiltr._Finish   = Convert.ToDateTime(txtStart.Text);
                return _PeriodFiltr._Finish;
            }
            catch
            {
                _PeriodFiltr._Finish = DateTimeTools.GetFisrtDayInMonth(DateTime.Now);
                return _PeriodFiltr._Finish;
            }

        }
        set {
            txtFinish.Text = value.ToShortDateString(); 
            _PeriodFiltr._Finish = value; }
    }

    public DateTimeTools.TimePeriod ShedulePeriod
    {
        get
        {
            switch (cmbTermCount.SelectedIndex)
            {
                case 0:
                    _PeriodFiltr._ShedulePeriod = DateTimeTools.TimePeriod.Month;
                    break;
                case 1:
                    _PeriodFiltr._ShedulePeriod = DateTimeTools.TimePeriod.Week;
                    break;
                case 2:
                    _PeriodFiltr._ShedulePeriod = DateTimeTools.TimePeriod.Day;
                    break;

            }
            return _PeriodFiltr._ShedulePeriod ;

        }
    }

    public bool ShowOnlySelected 
    {
        get 
        {
             return _PeriodFiltr._ShowOnlySelected  ;
            
        } 
        set 
        {
            _PeriodFiltr._ShowOnlySelected  = value; 
        } 
    }

    public string Lexeme
    {
        get { return txtLexeme.Text; }
        set { txtLexeme.Text = value; }
    }

    private Func<PeriodFiltr, bool> f_OnFiltrChanged;
    public Func<PeriodFiltr, bool> OnFiltrChanged { get { return f_OnFiltrChanged; } set { f_OnFiltrChanged = value; } }

    protected void btnFind_Click(object sender, ImageClickEventArgs e)
    {
        if (null != OnFiltrChanged) 
            OnFiltrChanged(_PeriodFiltr); 
    }
}
